<?php
require_once 'config/config.php';
require_once 'lib/member.php';
require_once 'lib/user.php';
require_once 'lib/kelas.php';

$config=new config();
$config->connect();

$Kelas_Dao=new Kelas_Dao();
$kelas=$Kelas_Dao->get_all();

if (ISSET($_POST["registrasi_member"]) ){

	$ernama = $eremail = $erpass1 = $erpass2 = $erkelas = $eremail2="";
	if($_POST['nama']=="") { 
		$ernama = "nama tidak boleh kosong.";
		}
	if($_POST['email']=="") { 
		$eremail = "email tidak boleh kosong.";
		}
	if($_POST['password1']=="") { 
		$erpass1 = "password tidak boleh kosong.";
		}
	if($_POST['password2']=="") { 
		$erpass2 = "password tidak boleh kosong.";
		}
	if($_POST['id_kelas']=="") { 
		$erkelas = "kelas tidak boleh kosong.";
		}
		
	
	/* if ($_POST['nama']=="" || $_POST['email']=="" || $_POST['password1']=="" || $_POST['password2']=="" || $_POST['id_kelas']==""){
		print "<script type=\"text/javascript\">alert('Belum diisi :\\n\\n$ernama \\n$eremail \\n$erpass1 \\n$erpass2 \\n$erkelas');</script>";
	} */

	if ($_POST['nama']!="" && $_POST['email']!="" && $_POST['password1']!="" && $_POST['password2']!="" && $_POST['id_kelas']!=""){
	
			if ($_POST['password1'] == $_POST['password2'] ){
		
				$Member_Dao=new Member_Dao();
				if($Member_Dao->cek_email($_POST['email'])==false){
				//echo $Member_Dao->cek_email($_POST['email']);
		
					$member = new Member();
					$member->email = $_POST['email'];
					$member->password = $_POST['password1'];
					$member->role = "member";
					$Member_Dao->add_user($member);
					
					$User_Dao = new User_Dao();
					$user = $User_Dao->get_ByEmail($_POST['email']);
				
					$member->id = $user->id;
					$member->nama = $_POST['nama'];
					$member->id_kelas = $_POST['id_kelas'];
					$member->fasilitas_umum = $_POST['fasilitas_umum'];
					$member->alamat = $_POST['alamat'];
					$member->no_telp = $_POST['no_telp'];
					$member->tgl_daftar = $_POST['tgl_daftar'];
					$member->banned = "0";
					$Member_Dao->add_member($member);
					$config->close();
					echo "<script>alert('Registrasi berhasil. Silahkan login menggunakan email dan password yang Anda daftarkan.!');
					location.href='login.php';
					</script>";
							
				} else { echo "<script>alert('Peringatan : Email tidak tersedia. Silahkan pilih email yang lain');</script>";}
			} else { echo "<script>alert('Peringatan : Kedua password tidak cocok!');</script>";}	
	} 
}

?>
<?php include("header.php") ?>
<?php include("menu_kiri.php") ?>

<div id="body">
<div class="topbox">
		Registrasi Member:
                  <br>
				  <br>
				  <form name="form1" method="post" action="registrasi.php">
				  <input type="hidden" name="registrasi_member">
				  <input type="hidden" name="tgl_daftar" value=<?php echo date("Y-m-d"); ?>>
                  <table class="table" cellspacing=0>
				   <tr>
					<td width="89">Nama Hotel*</td>
					<td width="3">:</td>
					<td width="218"><input type="text" name="nama" size=64 value="<?php echo isset($_POST['nama']) ? $_POST['nama'] : "";?>"></td>
					<td><div style="color:red"><?php echo isset($ernama) ? $ernama : '';?></div></td>
				    </tr>
				  <tr>
					<td>Email*</td>
					<td>:</td>
					<td><input type="text" name="email" size=64 value="<?php echo isset($_POST['email']) ? $_POST['email'] : '';?>"></td>
					<td><div style="color:red"><?php echo isset($eremail) ? $eremail : '';?></div>
					<div style="color:red"><?php echo isset($eremail2) ? $eremail2 : '';?></div>
					</td>
				    </tr>
				  <tr>
					<td>Password*</td>
					<td>:</td>
					<td><input type="password" name="password1" size=32 value="<?php echo isset($_POST['password1']) ? $_POST['password1'] : '';?>"></td>
					<td><div style="color:red"><?php echo isset($erpass1) ? $erpass1 : '';?></div></td>
				    </tr>
				  <tr>
					<td>Password (ketik kembali)*</td>
					<td>:</td>
					<td><input type="password" name="password2" size=32 value="<?php echo isset($_POST['password2']) ? $_POST['password2'] : '';?>"></td>
					<td><div style="color:red"><?php echo isset($erpass2) ? $erpass2 : '';?></div></td>
				    </tr>
				  <tr>
					<td>Kelas Hotel*</td>
					<td>:</td>
					<td>
					<select name="id_kelas"> 
					<option value="">Pilih kelas hotel</option>
					<?php
						foreach($kelas as $Kelas){
					?>
						<option value="<?php echo $Kelas->id_kelas;?>"><?php echo $Kelas->kelas;?></option>
					
						<?php }?>
					</select></td>
					<td><div style="color:red"><?php echo isset($erkelas) ? $erkelas : '';?></div></td>
				    </tr>
				  <tr>
					<td>Fasilitas</td>
					<td>:</td>
					<td><textarea name="fasilitas_umum"  cols=48.5></textarea></td>
				
				    </tr>
				  <tr>
					<td>Alamat</td>
					<td>:</td>
					<td><input type="text" name="alamat" size=64></td>
				    </tr>
				  <tr>
					<td>No Telp</td>
					<td>:</td>
					<td><input type="text" name="no_telp"></td>
				  </tr>
				  </table>
				  <br>
				  <input type="submit" name="submit" value="submit" onclick="return validasi();">
				  </form>               
</div>  
<div class="topbox">
* Data harus diisi.
</div>           
</div>
          
         <div class="clear"></div>
         <?php include("footer.php") ?>
<script>
function validasi(){
	var x = document.form1.nama.value;
	var y = document.form1.email.value;
	var z = document.form1.password1.value;
	var a = document.form1.password2.value;
	var b = document.form1.id_kelas.value;
	
	if(x==""||y==""||z==""||a==""|| b==""){
		alert('Isikan data terlebih dahulu');
		return false;
	}
}
</script>

		